package day10;

import java.io.File;
import java.util.LinkedList;

/**
 * @author oldliu
 * @since 1.0
 */
public class CalcFileSize {
    //打印名字
    public static void calc(File f) {
        System.out.println(f.getAbsolutePath());
        File[] files = f.listFiles();
        if (files == null || files.length == 0)
            return;
        for (File t : files) {
            calc(t);
        }
    }

    public static void calc2(File f) {
        LinkedList<File> files = new LinkedList<>();
        files.add(f);
        long size = 0;
        while (files.size() > 0) {
            File file = files.removeFirst();//删除第一个，并返回
            if (file.isFile()) {
                size += file.length();
                continue;
            }
            File[] ar = file.listFiles();
            for (File t : ar) {
                files.addLast(t);
            }
        }
        System.out.println(f.getName() + "大小：" + size + "字节");
    }

    public static void main(String[] args) {
        String fileName = "F:\\java6.0\\1.Java语法基础";
        //calc(new File(fileName));
        calc2(new File(fileName));//43,642,862
    }
}
